package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.junit.Assert.assertThat;

public class AddContactTests extends SuperClass {

	Random rnd = new Random();

	@DataProvider(name = "randomContacts")
	public Iterator<Object[]> generateRandomContacts() {
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 3; i++) {
			ContactObject contact = new ContactObject()
					.setFName("first_name" + rnd.nextInt())
					.setLName("last_name" + rnd.nextInt())
					.setAddress("address" + rnd.nextInt())
					.setHome("home" + rnd.nextInt())
					.setMobile("mobile" + rnd.nextInt())
					.setWork("work" + rnd.nextInt())
					.setEmail("email" + rnd.nextInt())
					.setEmail2("email2" + rnd.nextInt()).setBYear("6666")
					.setBDay("3").setBMonth("May")
					.setNewGroup("new_group" + rnd.nextInt())
					.setPhone2("phone2" + rnd.nextInt())
					.setAddress2("address_sec" + rnd.nextInt());

			list.add(new Object[] { contact });
		}
		return list.iterator();
	}

	@Test(dataProvider = "randomContacts")
	public void testAddContactTrue(ContactObject addContact) throws Exception {
		Set<ContactObject> existList = app.getContactHelper().getContacts();
		// action
		app.getContactHelper().createContact(addContact);
		// verification
		Set<ContactObject> newList = app.getContactHelper().getContacts();
		verifyContactAdded(existList, addContact, newList);
	}

	private void verifyContactAdded(Set<ContactObject> existList,
			ContactObject addContact, Set<ContactObject> newList) {
		assertThat(newList.size(), equalTo(existList.size() + 1));
		addContact.setId("$$$");
		existList.add(addContact);
		addContact.setId(null);
		assertThat(newList, equalTo(existList));

	}

	/*
	 * hamcrest style
	 * 
	 * @Test(dataProvider = "randomContacts") public void
	 * testAddContactTrue(ContactObject addContact) throws Exception {
	 * Set<ContactObject> existList = app.getContactHelper().getContacts(); //
	 * action app.getContactHelper().createContact(addContact); // verification
	 * Set<ContactObject> newList = app.getContactHelper().getContacts();
	 * hamcrestVerify(existList, addContact, newList);
	 * 
	 * }
	 * 
	 * public void hamcrestVerify(Set<ContactObject> existList, ContactObject
	 * addContact, Set<ContactObject> newList) { assertThat(newList,
	 * is(equalTo(existList.withAdded(addContact))); }
	 * 
	 * private void withAdded(Set<ContactObject> existList, ContactObject
	 * addContact) { addContact.setId("$$$"); existList.add(addContact);
	 * addContact.setId(null); }
	 */

	@Test
	public void testContactWithEmptyFieldsTrue() throws Exception {
		ContactObject addContact = new ContactObject().setFName("")
				.setLName("").setAddress("").setHome("").setMobile("")
				.setWork("").setPhone2("").setEmail("").setEmail2("")
				.setBYear("").setAddress2("").setBDay("").setBMonth("")
				.setNewGroup("");
		app.getContactHelper().createContact(addContact);
	}

}
